package ch.fhnw.qualiscan.core.db.fieldfactories;

import java.util.List;

import ch.fhnw.qualiscan.application.models.LanguageModel;
import ch.fhnw.qualiscan.application.models.UserModel;
import ch.fhnw.qualiscan.core.db.HibernateSession;
import ch.fhnw.qualiscan.core.db.pojos.Language;
import ch.fhnw.qualiscan.core.helpers.SessionHelper;

import com.vaadin.data.Item;
import com.vaadin.data.Validator;
import com.vaadin.data.util.BeanItemContainer;
import com.vaadin.data.validator.EmailValidator;
import com.vaadin.ui.ComboBox;
import com.vaadin.ui.Component;
import com.vaadin.ui.Field;
import com.vaadin.ui.FormFieldFactory;
import com.vaadin.ui.TextField;

import eu.livotov.tpt.i18n.TM;

/**
 * Creates the Fields for User pojos in forms.
 * @author Kevin Mettenberger
 *
 */
@SuppressWarnings("serial")
public class UserFieldFactory implements FormFieldFactory {
	
	public HibernateSession hs;
	Validator passwordValidator;
	Validator emailValidator;
	
	public UserFieldFactory() {
		emailValidator = new Validator() {
			public void validate(Object value) throws InvalidValueException {
				if(!isValid(value)) {
					throw new InvalidValueException(TM.get("user.error.emailexists"));
				}
			}

			public boolean isValid(Object value) {
				UserModel userModel = new UserModel();
			
				if(userModel.isEmailValid(value.toString())) {
					return true;
				} else {
					return false;
				}
			}
			
		};
	}
	
	public Field createField(Item item, Object propertyId, Component uiContext) {
		String pid = (String) propertyId;
		
		if (pid.equals("firstname")) {
			TextField firstName = new TextField(TM.get("user.firstname"));
			firstName.setRequired(true);
			firstName.setRequiredError(TM.get("user.error.firstname"));
			firstName.setWidth("280px");
            return firstName;
        } else if (pid.equals("lastname")) {
			TextField lastName = new TextField(TM.get("user.lastname"));
			lastName.setRequired(true);
			lastName.setRequiredError(TM.get("user.error.lastname"));
			lastName.setWidth("280px");
            return lastName;
        }
        else if (pid.equals("language")) {
        	LanguageModel lm = new LanguageModel();
        	List<Language> listLang = lm.getLanguages();
        	BeanItemContainer<Language> bic = new BeanItemContainer<Language>(Language.class);
        	
        	for(int i = 0; i < listLang.size();i++) {
        		bic.addBean(listLang.get(i));
        	}
   
        	ComboBox language = new ComboBox(TM.get("user.language"));
        	language.setContainerDataSource(bic);
        	language.setItemCaptionPropertyId("language");
        	language.setRequired(true);
        	language.setRequiredError(TM.get("user.error.language"));
        	language.setImmediate(true);
        	language.setNullSelectionAllowed(false);
        	language.select(bic.firstItemId());
        	
        	return language;
        } 
        else if(pid.equals("email")) {
        	TextField email = new TextField(TM.get("user.email"));
        	email.setRequired(true);
        	email.setRequiredError(TM.get("user.error.email"));
        	email.addValidator(new EmailValidator(TM.get("user.error.emailinvalid")));
        	email.addValidator(emailValidator);
        	email.setWidth("280px");
        	return email;
        }
		
		return null;
	}
}